package com.ruoyi.web.cp.service.http;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.CharsetUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.web.core.config.ConfigLoader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Slf4j
@Service
public class RmHttpService {
    @Autowired
    private ConfigLoader configLoader;

    /**
     * @Description: 获取rm请求头
     * @Author: xiesm
     * @Date: 2025/3/21
     */
    public JSONObject getRmHeaders() {
        List<String> cookie = FileUtil.readLines(configLoader.getGamenamesPath() + "rm-cookie.txt", "utf8");
        JSONObject header = new JSONObject();
        header.put("cookie", cookie);
        return header;
    }

    /**
     * @Description: 获取rm列表
     * @Author: xiesm
     * @Date: 2025/3/21
     */
    public synchronized String sendRmList() {
        return HttpUtils.httpStr(ConfigLoader.getConfig().getRmHost() + "/market.php", "get", null, getRmHeaders());
    }

    /**
     * @Description: 获取rm详情
     * @Author: xiesm
     * @Date: 2025/3/21
     */
    public synchronized String sendRmDetail(String id, String game) {
        String filePath = configLoader.getGamenamesPath() + "rm-gamescore/" + id + ".txt";
        boolean fileExist = FileUtils.isFileExist(filePath);
        if (fileExist) {
            String str = FileUtil.readString(filePath, CharsetUtil.charset("utf8"));
            if (str.length() > 1000) {
                return str;
            }
        }
        log.info(game);
        return readRmDetail(id, filePath);
    }

    public void updateRmDetail(String id, String game) {
        String filePath = configLoader.getGamenamesPath() + "rm-gamescore/" + id + ".txt";
        File file = new File(filePath);

        // 文件存在且在过去一小时内修改过，则直接返回文件内容
        if (file.exists()) {
            long lastModifiedTime = file.lastModified();
            long currentTime = System.currentTimeMillis();
            // 检查是否在过去1小时内
            if (currentTime - lastModifiedTime <= 50 * 60 * 1000) {
                return;
            }
        }

        log.info("update_rm_detail:{}", game);
        readRmDetail(id, filePath);
    }

    public String readRmDetail(String id, String filePath) {
        long beginTime = System.currentTimeMillis();
        String str = HttpUtils.httpStr(ConfigLoader.getConfig().getRmHost() + "/marketorder.php?gameId=" + id, "get", null, getRmHeaders());
        FileUtil.writeString(str, filePath, "utf8");
        log.info("耗时:{}", (System.currentTimeMillis() - beginTime));
        ThreadUtil.sleep(15000);
        return str;
    }

    public String readRmResult(String dateStr) {
        String str = HttpUtils.httpStr(ConfigLoader.getConfig().getRmHost() + "/gameresult.php?day=" + dateStr, "get", null, getRmHeaders());
        ThreadUtil.sleep(15000);
        return str;
    }

    public String readRmCharShow(String gameId, String marketId) {
        JSONObject body = new JSONObject();
        body.put("gameId", gameId);
        body.put("marketId", marketId);
        String str = HttpUtils.httpStr(ConfigLoader.getConfig().getRmHost() + "/chatShow.php", "post", body, getRmHeaders(), "x-www-form-urlencoded");
        ThreadUtil.sleep(30000);
        return str;
    }
}
